<?php
namespace app\index\controller;
use app\admin\model\NewsModel;
use app\admin\model\ConfigModel;
use think\Db;
class Note extends Base
{
    /**
     * 留言
     * @param int $user_id
     * @param string $content
     * @return \think\response\Json
     */
    public function add(){
        if(request()->isAjax()){
            $param = input('post.');
            if(!$param['uid']){
                return json(['code'=>0,'msg'=>'用户不能为空']);
            }
            $user_id = $param['uid'];
            if(!$param['content']){
                return json(['code'=>0,'msg'=>'留言不能为空']);
            }
            $content = $param['content'];
            $name = $this->user_id.'U'.$user_id;

            Db::startTrans();

            $room_id = Db('room')->where('name',$name)->value('id');  //查看房间是否创建
            if(!$room_id){
                $room_id = Db('room')->where('name',$user_id.'U'.$this->user_id)->value('id');
                if(!$room_id){   //没有就创建
                    $room_id = Db('room')->insertGetId(['name'=>$name,'create_time'=>time(),'update_time'=>time()]);
                }
            }
            $note_id = Db('note')->insertGetId(['room_id'=>$room_id,'user_id'=>$this->user_id,'to_user_id'=>$user_id,'content'=>$content,'create_time'=>time(),'update_time'=>time()]);
            $update = Db('room')->where('id',$room_id)->update(['last_id'=>$note_id,'update_time'=>time()]);

            if($room_id && $note_id && $update){
                Db::commit();
                return json(['code'=>1,'msg'=>'留言成功']);
            }else{
                Db::rollback();
                return json(['code'=>0,'msg'=>'留言失败']);
            }
        }
        $uid = input('param.uid');
        $this->assign('uid',$uid);
        return $this->fetch();

    }

    /**
     * 留言记录
     * @param int $user_id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info($room_id=0){
        if(!$room_id){
            return json(['code'=>0,'msg'=>'信息缺失']);
        }
        $list = Db('note')
            ->alias('a')
            ->join('think_member b','a.user_id=b.id')
            ->field('user_id,to_user_id,content,a.create_time,b.head_img as user_head_img')
            ->order('a.create_time')
            ->where(['room_id'=>$room_id])
            ->select();
        if($list){
            foreach ($list as &$v){
                $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                if(!strpos($v['user_head_img'],'ttp')){
                    $v['user_head_img'] = 'http://' . $_SERVER['SERVER_NAME'].'/uploads/face/'.$v['user_head_img'];
                }

            }
        }
//        echo '<pre>';
//        print_r($list);die;
        $res = Db('note')->where(['room_id'=>$room_id,'to_user_id'=>$this->user_id,'is_read'=>0])->field('id')->select();  //消息已读
        if($res){
            foreach ($res as $v){
                Db('note')->where('id',$v['id'])->update(['is_read'=>1]);
            }
        }

        $this->assign('to_uid',Db('note')->where('room_id',$room_id)->value('to_user_id'));
        $this->assign('uid',$this->user_id);
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * @param int $me  默认1=>我留言的人,2=>给我留言的人
     * @param int $page
     * @return \think\response\Json
     */
    public function noteList($me=1){
        if($me == 1){   //我留言的
            $where['user_id'] = $this->user_id;
            $field = 'd.nickname,d.head_img';
        }else{   //给我留言的
            $where['to_user_id'] = $this->user_id;
            $field = 'c.nickname,c.head_img';
        }
//        print_r($where);die;
        $list = Db('room')
            ->alias('a')
            ->join('note b','a.last_id=b.id')
            ->join('member c','b.user_id=c.id')
            ->join('member d','b.to_user_id=d.id')
            ->where($where)
            ->field('content,is_read,b.create_time,room_id,'.$field)
            ->group('b.room_id')
            ->order('b.create_time desc')
            ->select();
//        echo '<pre>';
//        print_r($list);die;
        if($list){
            foreach($list as &$v){
                $v['is_read'] = $v['is_read'] ? '<span style="color:green">已读</span>' : '<span style="color:red">未读</span>';
                $v['create_time'] = date('m-d',$v['create_time']);
                if(!strpos($v['head_img'],'ttp')){
                    $v['head_img'] = 'http://' . $_SERVER['SERVER_NAME'].'/uploads/face/'.$v['head_img'];
                }

            }
        }
        $this->assign('list',$list);
        return $this->fetch('list');
    }



}
